#!/bin/bash
# notice 在当前路径下运行次脚本
# notice change.txt文件没有进行大小判断 状态反复较多的话会产生一个大文件 有兴趣可自行修改
# notice  当前目录下是否有change.txt  和 down.txt 以免产生事故
rm -rf down.txt
>change1.txt
>change2.txt
>shicha.txt
while :
do
	dt=`date `
	dti=`date +%s`
	nc -z 172.16.145.176 21 &>/dev/null
	a="$?"
	if (( a == 0 )) 
	then
		echo " $dt  ftp is runing"
		runing=`date +%s`
	else	
		if [ ! -e down.txt  ]
		then
			 echo " $dt ftp is down" >>down.txt
			echo " $dt ftp is down"
			downtime=`date +%s`
		else
			echo " $dt ftp is down"
		fi
	fi
	sleep 1
	nc -z 172.16.145.176 21  &>/dev/null
	b="$?"
	if [[ "$a" != "$b"  ]]
	then
		size1=`du change1.txt|awk '{print $1}'`
		if  (( "$a" == 0 ))
		then	
			now1=`date +%s`
			now111=`date`
			(( shicha11=$now1-$downtime  ))  &>/dev/null
			echo "$now111 open to down $shicha11"  >>shicha.txt
		else
			now1=`date +%s`
			now111=`date`
                        (( shicha11=$now1-$runing  ))   &>/dev/null
                        echo "$now111 down  to open $shicha11"  >>shicha.txt
		fi
		if (( $size1 <= 100000 ))
		then
			if (( "$a" == 0 ))
			then
				echo "${a}:${b}:${dt}:${downtime}:ftpdown"  >>change1.txt
			else
				echo "${a}:${b}:${dt}:${dti}:ftpopen"  >>change2.txt
			fi
		fi
	fi
done











